package es.unileon.prg2.censo.iterator;

/**
 * @author Manuel Molinos Crespo, Adrian Campazas Vega, Alberto Castro Delgado, Pedro Alvarez Alvarez, Adrian Cabello Galvan, Jose Alvaro Fernandez Alvarez
 *
 */


import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import es.unileon.prg2.censo.adapter.Aggregate;
import es.unileon.prg2.censo.adapter.ArrayListAggregate;
import es.unileon.prg2.censo.composite.Component;
import es.unileon.prg2.censo.composite.Country;
import es.unileon.prg2.censo.exception.CensoException;
import es.unileon.prg2.censo.handler.DNICitizen;
import es.unileon.prg2.censo.handler.LongName;

public class CitizenIterator implements Iterador {
	static Logger logger = Logger.getLogger(DNICitizen.class.getName());

	private int position, min, max;
	private ArrayList<Component> element = new ArrayList<Component>();

	
	/**
	 * Constructor used to created an Iterator that only catch citizens into of the ArrayList. It catchs only the citizens that meet the age condition.
	 * When you create an CitizenIterator, you need say to constructor the root of the tree and min and max age of the citizen.
	 * @param boss ArrayList with all components of the censo
	 * @param min Age minimal for created the age condition 
	 * @param max Age maximal for created the age condition 
	 */
	public CitizenIterator(ArrayListAggregate<Component> arrayListAggregate,int age, int age2) {
		PropertyConfigurator.configure("etc/log4j.properties");
		
		this.position = 0;	
		if(age>age2) {
			this.setMin(age2);
			this.setMax(age);
		} else {
			this.setMax(age2);
			this.setMin(age);
		}
		for(int i=0; i < arrayListAggregate.size();i++) {
			if(((Component) arrayListAggregate.getElement(i)).getComposite() == null && ((Component) arrayListAggregate.getElement(i)).getAge() <= this.max && ((Component) arrayListAggregate.getElement(i)).getAge() >= this.min) {
				Component prueba = ((Component) arrayListAggregate.getElement(i));
				this.element.add(prueba);
			}
		}
	}

	@Override
	public void firstElement() {
		this.position = 0;
	}

	@Override
	public void nextElement() throws CensoException {
		if (this.hasmoreElements() == true) {
			this.position++;
		} else {
			StringBuffer message = new StringBuffer();
			message.append("Haven't more elements in this tree.");
			logger.error("Haven't more elements in this tree."); 
			
			if (message.length() != 0) {
				throw new CensoException(message.toString());
			}
		}
	}

	@Override
	public boolean hasmoreElements() throws CensoException {
		return this.element.size() >= this.position;
	}
	
	public int getSize(){
		return this.element.size();
	}

	@Override
	public Object currentElement() {
		return this.element.get(this.position);
	}
	
	/**
	 * Method used to find the citizens that meet the age condition into the ArrayList.
	 * @param boss ArrayList with all components of the censo
	 */

	public int getMin() {
		return this.min;
	}

	public void setMin(int min) {
		this.min = min;
	}

	public int getMax() {
		return this.max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	@Override
	public int getPosition() {
		return this.position;
	}
	
	public int getSizeIterator() {	
		return this.element.size();
	}

}
